// 百度地图API功能
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(91.9,27.9),8);
map.addEventListener("click", function(e){
    var geocoder= new BMap.Geocoder();
    // alert(e.point.lng + ", " + e.point.lat);
    $('#lng').val(e.point.lng);
    $('#lat').val(e.point.lat);
    geocoder.getLocation(e.point,function(rs){
        $('#address').val(rs.address);
        console.log(rs.address); //地址描述(string)

        console.log(rs.addressComponents);//结构化的地址描述(object)
        console.log(rs.addressComponents.province); //省
        console.log(rs.addressComponents.city); //城市
        console.log(rs.addressComponents.district); //区县
        console.log(rs.addressComponents.street); //街道
        console.log(rs.addressComponents.streetNumber); //门牌号

        console.log(rs.surroundingPois); //附近的POI点(array)

        console.log(rs.business); //商圈字段，代表此点所属的商圈(string)
    });
});
setTimeout(function(){
    map.setZoom(12);
}, 2000);  //2秒后放大到14级
map.enableScrollWheelZoom(true);

function itermData() {
    var data = []
    data.push($('#lng').val(),$('#lat').val(),$('#address').val())
    return data;
}
function itermClose() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

function searchAddress() {
    /*var options = {
        onSearchComplete: function(results){
            if (local.getStatus() == BMAP_STATUS_SUCCESS){
                // 判断状态是否正确
                var s = [];
                for (var i = 0; i < results.getCurrentNumPois(); i ++){
                    s.push(results.getPoi(i).title + ", " + results.getPoi(i).address);
                }
                // document.getElementById("log").innerHTML = s.join("<br>");
            }
        }
    };*/
    // var local = new BMap.LocalSearch(map, options);
    var local = new BMap.LocalSearch(map, {
        renderOptions:{map: map},
        onSearchComplete: function(results){
            if (local.getStatus() == BMAP_STATUS_SUCCESS){
                // 判断状态是否正确
                var s = [];
                for (var i = 0; i < results.getCurrentNumPois(); i ++){
                    s.push(results.getPoi(i).address + " " + results.getPoi(i).title);
                    $('#lng').val(results.getPoi(i).point.lng);
                    $('#lat').val(results.getPoi(i).point.lat);
                }
                $('#address').val(s);
                // document.getElementById("log").innerHTML = s.join("<br>");
            }
        }
    });
    var address = $('#searchName').val()
    local.search(address);
}